#!/usr/bin/python
# -*- coding:utf-8 -*-
#scriptName OF3D_zchPipePublicClass.py
#
#import OF3D_PublicClass.OF3D_zchPipePublicClass as OF3D_zchPipePublicClass
#reload(OF3D_zchPipePublicClass)

import maya.cmds as mc
import maya.mel as mel
import maya.OpenMaya as OpenMaya
import time
import sys,re,os
import random

import tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.attributes
reload(tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.attributes)

import tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.fileDialog
reload(tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.fileDialog)

import tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.ikspline
reload(tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.ikspline)

import tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.mayaPrint
reload(tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.mayaPrint)

import tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.objectName
reload(tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.objectName)

import tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.uiAbout
reload(tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.uiAbout)

import tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.pointOnCurve
reload(tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.pointOnCurve)

import tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.searchByName
reload(tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.searchByName)

import tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.shapeName
reload(tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.shapeName)

import tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.mesh
reload(tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.mesh)

import tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.qualityAssurance
reload(tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.qualityAssurance)

import tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.hierarchy
reload(tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.hierarchy)

import tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.strings
reload(tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.strings)

class PublicClass( 
        tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.attributes.AttributeManage ,
        tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.strings.Strings,
        tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.fileDialog.FDialog,
        tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.ikspline.IkSpline,
        tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.mayaPrint.MayaPrint ,
        tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.objectName.Naming,
        tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.pointOnCurve.PointOnCurve,
        tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.searchByName.Matching,
        tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.uiAbout.UIAbout,
        tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.shapeName.ShapeName,
        tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.mesh.MeshShape,
        tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.qualityAssurance.QualityAssurance,
        tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.hierarchy.Hierarchy):
    
    def __init__(self):
        tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.qualityAssurance.QualityAssurance.__init__(self)
        tools_Persional.LZ_Tools.LZ_Tools.OF3D_PublicClass.mayaPrint.MayaPrint.__init__(self)
        
        self.mayaVersion = mc.about(f=True)
        self.userName = os.environ['USERNAME']
        
    def dltGeoAndAddToBlendshape(self,baseGeo,targetName,itsBlends=None):
        
        #baseGeo = self.headGeo
        itsSkin=None
        getSkinCluster = mc.ls(mc.listHistory(baseGeo),type="skinCluster")
        if getSkinCluster!=[]:
            for ski in getSkinCluster:
                mc.setAttr("%s.envelope"%(ski),0)
        getBlendShape = mc.ls(mc.listHistory(baseGeo),type="blendShape")
        if getBlendShape!=[]:
            for bls in getBlendShape:
                mc.setAttr("%s.envelope"%(bls),0)
        line = mc.duplicate(baseGeo,name=targetName)[0]
        #set Attribute vis=0 unlock channels 
        mc.setAttr("%s.visibility"%(line),0)
        for atr in ["tx","ty","tz","rx","ry","rz","sx","sy","sz"]:
            mc.setAttr("%s.%s"%(line,atr),lock=False)
        if getSkinCluster!=[]:
            for ski in getSkinCluster:
                mc.setAttr("%s.envelope"%(ski),1)
        if getBlendShape!=[]:
            for bls in getBlendShape:
                mc.setAttr("%s.envelope"%(bls),1)
        shapeOrig = self.removeShapeOrig(line)
        if itsBlends!=None:
            counts = mc.blendShape(itsBlends,q=1,wc=1)
            #whatIs bsMultiIndexForTarget //run it in mel scripts edit
            mi = mel.eval("""bsMultiIndexForTarget("%s",%d)"""%(itsBlends,counts-1))
            if mi==-1:
                mi = counts
            else:
                mi = mi+1
            mc.blendShape(itsBlends,e=1,target=[baseGeo,mi+1,line,1])
        mc.select(cl=True)
        return line
        
    def makeWithSamePlane(self,aJointList,aimVector=[1,0,0],upVector=[0,-1,0]):
        
        if len(aJointList)!=3 and type(aJointList)!=list:
            self.mayaError("no 1 list with 3 names.")
            return
        mc.parent(aJointList[1],aJointList[2],w=True)
        tempGrp = mc.createNode("transform",name="OF3D_orientJontUp_grp")
        mc.delete(mc.pointConstraint(aJointList[0],aJointList[2],tempGrp,offset=[0,0,0],weight=1))
        aimCon = mc.aimConstraint(aJointList[2],aJointList[1],offset=[0,0,0],weight=1,aimVector=aimVector,upVector=upVector,worldUpType="object",worldUpObject=tempGrp)
        mc.delete(aimCon)
        aimCon = mc.aimConstraint(aJointList[1],aJointList[0],offset=[0,0,0],weight=1,aimVector=aimVector,upVector=upVector,worldUpType="object",worldUpObject=tempGrp)
        mc.delete(aimCon)
        mc.makeIdentity(aJointList[0],apply=1,t=0,r=1,s=0,n=0)
        mc.makeIdentity(aJointList[1],apply=1,t=0,r=1,s=0,n=0)
        mc.parent(aJointList[2],aJointList[1])
        mc.parent(aJointList[1],aJointList[0])
        mc.delete(tempGrp)

    def removeUnusedInfluence(self,skinClusterName,printer=True):
        """remove skinCluster Unused Influence 
        """
        amount = 0
        infls = mc.skinCluster(skinClusterName,q=True,inf=True)
        wtinfs = mc.skinCluster(skinClusterName,q=True,wi=True)
        #have no use for runing down.
        if infls==None or wtinfs==None or infls==wtinfs:
            return
        maxVal = len(infls)-len(wtinfs)
        if maxVal<=0:
            return
        #Funtion Main---------------------------------------
        mc.progressWindow(title='',progress=amount,status='Remove Unused Influence...',isInterruptable=True,maxValue=maxVal)
        nodeState = mc.getAttr("%s.nodeState"%(skinClusterName))
        lock = mc.getAttr("%s.nodeState"%(skinClusterName),lock=1)
        mc.setAttr("%s.nodeState"%(skinClusterName),1)
        mc.setAttr("%s.nodeState"%(skinClusterName),lock=False)
        for jnt in infls:
            if mc.progressWindow( query=True, isCancelled=True ) :
                break
            if jnt not in wtinfs:
                mc.skinCluster(skinClusterName,e=True,ri=jnt)
                mc.progressWindow(e=True,progress=amount)
                amount += 1
                if printer:
                    print "skinCluster -e -ri %s %s;"%(jnt,skinClusterName)
        mc.progressWindow(endProgress=1)
        mc.setAttr("%s.nodeState"%(skinClusterName),nodeState)
        mc.setAttr("%s.nodeState"%(skinClusterName),lock=lock)
        
PubCls = PublicClass()
